package com.buka.recipe.security.token;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class WechatAuthenticationToken extends AbstractAuthenticationToken {

    private final Object principal;
    private Object credentials;

    /**
     * 未认证的Token
     *
     * @param code code
     */
    public WechatAuthenticationToken(String code) {
        super(null);
        this.principal = code;
        setAuthenticated(false);
    }

    /**
     * 已认证的token
     *
     * @param code        code
     * @param authorities 权限
     */
    public WechatAuthenticationToken(String code, Object credentials, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = code;
        this.credentials = credentials;
        setAuthenticated(true);
    }

    /**
     * 未认证的Token
     *
     * @param code code
     */
    public static WechatAuthenticationToken unauthenticated(String code) {
        return new WechatAuthenticationToken(code);
    }

    /**
     * 已认证的token
     *
     * @param code        code
     * @param authorities 权限
     */
    public static WechatAuthenticationToken authenticated(String code, Object credentials, Collection<? extends GrantedAuthority> authorities) {
        return new WechatAuthenticationToken(code, credentials, authorities);
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }
}
